import requests

"""
data中语言的格式：
1  ->  AUTO
2  ->  zh
4  ->  en
9  ->  ja
18  ->  de
20  ->  fr
"""

class TranslateModule:

    def __init__(self):
        """
        翻译模块
        """

        self.headers = {
            "accept": "* / *",
            "accept - encoding": "gzip, deflate, br",
            "accept - language": "zh - CN, zh;q = 0.9",
            "content - length": 24,
            "content - type": "application / x - www - form - urlencoded",
            "origin": "https: // www.woaifanyi.com",
            "referer": "https: // www.woaifanyi.com /",
            "user - agent": "Mozilla / 5.0(WindowsNT6.1;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 63.0.3239.132Safari / 537.36",
        }
        self.url = "https://www.woaifanyi.com/api/2.0/save/?ajaxtimestamp=1685102193671"

    def post_request(self, data: dict):
        """
        获取结果
        :param data:
        :return:
        """

        r = requests.post(url=self.url, headers=self.headers, data=data, verify=False).text
        res = ""
        try:
            print(r)
            r = r.split("1,\"n_r\":\"")[1].split("\"},{\"")[0]
            res = r.encode().decode('unicode_escape')
        except Exception as e:
            print("Error Happens While Splitting: ", e)
        return res


if __name__ == "__main__":
    translate = TranslateModule()
    data = {
        "source": "hello",
        "from": 1,
        "to": 4,
    }
    print(translate.post_request(data))